package com.ymatou.iapi.couponactivity.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponactivity.parameter.BoundCouponBean;
import com.ymatou.iapi.couponactivity.service.BoundCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.CouponServiceCallService;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponActivity;
import com.ymttest.database.model.YmtCouponActivityBatch;
import com.ymttest.database.model.YmtCouponActivityNewUserBound;
import com.ymttest.database.model.YmtCouponActivityPregenerate;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.model.YmtCouponVirtualRelation;
import com.ymttest.database.sqlwapper.YmtCouponActivityWapper;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/**
 * 绑定优惠券用例 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_BoundCoupon {
	private static BoundCouponBean boundcouponBean;
	private static BoundCouponCall boundcouponCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("绑定优惠券用例");
	}

	@Before
	public void caseUp() {
		boundcouponBean = new BoundCouponBean();
		boundcouponCall = new BoundCouponCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(boundcouponCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BoundCoupon_001() {
		Logger.start(true, "绑定未绑定优惠券-SELECT *  FROM [YmtRelease].[dbo].[Ymt_CouponActivityBatch]   where [ActivityCode]=16 and [SendItemNo]=1 中CouponBatchCodes需要改成有效批次");
		try {

			List<YmtCouponActivityBatch> batchs = new YmtCouponActivityWapper()
					.selectYmtCouponActivityBatchByActivityCode(16);

			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();

			String batchCode = "";

			for (YmtCouponActivityBatch activityBatch : batchs) {
				String batchCodeStr = activityBatch.getCouponBatchCodes();
				String batchCodes[] = batchCodeStr.split(",");
				batchCode = batchCodes[batchCodes.length - 1];

				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper()
						.selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}

			List<YmtCouponActivityPregenerate> ymtCouponActivityNewUserBounds = new YmtCouponActivityWapper()
					.selectTop1UnUsedPregenerateCodeByActivityCode(16);

			int userId = 30000000 + Integer.parseInt(TestDataManager.getRandomNum(7));

			boundcouponBean.setUserId(userId);
			boundcouponBean.setCouponCode(ymtCouponActivityNewUserBounds.get(0).getPreCouponCode());

			boundcouponCall.setData(boundcouponBean);
			boundcouponCall.callService();

			Logger.verifyEquals("0", boundcouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("操作成功！", boundcouponCall.getMessage(), "验证返回Message");

			// 验证数据库落地
			// YmtCouponActivityNewUserBound表
			List<YmtCouponActivityNewUserBound> bounds = new YmtCouponActivityWapper()
					.selectCouponActivityNewUserBoundByUserIdAndActivityCode(userId+"", 16);

			Logger.verifyEquals(1, bounds.size(), "验证数据库表Ymt_CouponActivityNewUserBound生成一条绑定记录");

			if (bounds.size() == 1) {
				YmtCouponActivityNewUserBound bound = bounds.get(0);
				// Logger.verifyEquals(1,
				// bound.getCouponSendStatus().intValue(),
				// "验证数据库表Ymt_CouponActivityNewUserBound中字段CouponSendStatus为1");
				Logger.verifyEquals(userId, Integer.parseInt(bound.getUserId()),
						"验证数据库表Ymt_CouponActivityNewUserBound中字段UserId");

				// 已发放
				Logger.verifyEquals(2, bound.getCouponSendStatus().intValue(),
						"验证数据库表Ymt_CouponActivityNewUserBound中字段CouponSendStatus为1");

				for (int i = 0; i < settingIdlist.size(); i++) {
					YmtCouponSetting setting = new YmtCouponSettingWapper()
							.selectByiCouponSettingId(settingIdlist.get(i)).get(0);
					Logger.verifyEquals(1, setting.getiReceiveCount() - receiveCountlist.get(i),
							"验证返回Ymt_CouponSetting表iSettingId=" + settingIdlist.get(i) + "的记录中iReceiveCount多了1");
				}

				for (YmtCouponActivityBatch activityBatch : batchs) {
					YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);

					List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper()
							.selectByBatchIdAndUserId(batch.getiBatchId(), userId);
					Logger.verifyEquals(1, ymtCouponPrivateUserBounds.size(), "验证返回YmtCouponPrivateUserBound生成一条记录");

					List<YmtCoupon> coupons = new YmtCouponWapper()
							.selectBysCouponCode(ymtCouponPrivateUserBounds.get(0).getsCouponCode());
					Logger.verifyEquals(1, coupons.size(), "验证返回YmtCoupon生成一条记录");

					// 验证Ymt_CouponVirtualRelation表
					List<YmtCouponVirtualRelation> couponVirtualRelations = new YmtCouponActivityWapper().selectCouponVirtualRelationByCouponCodeAndVirtualCouponCode(ymtCouponPrivateUserBounds.get(0).getsCouponCode(), ymtCouponActivityNewUserBounds.get(0).getPreCouponCode());
					Logger.verifyEquals(1, couponVirtualRelations.size(), "验证Ymt_CouponVirtualRelation生成一条记录");

					if(couponVirtualRelations.size()==1)
					{
						YmtCouponVirtualRelation relation = couponVirtualRelations.get(0);
						
						Logger.verifyEquals(false, relation.getIsUsed(), "验证Ymt_CouponVirtualRelation中IsUsed");
						Logger.verifyEquals(false, relation.getIsCancel(), "验证Ymt_CouponVirtualRelation中IsCancel");
					}
					
				}
				
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BoundCoupon_002() {
		Logger.start(true, "绑定已绑定优惠券");
		try {
			
			List<YmtCouponActivityPregenerate> ymtCouponActivityNewUserBounds = new YmtCouponActivityWapper()
					.selectTop1UsedPregenerateCodeByActivityCode(16);

			int userId = 30000000 + Integer.parseInt(TestDataManager.getRandomNum(7));

			boundcouponBean.setUserId(userId);
			boundcouponBean.setCouponCode(ymtCouponActivityNewUserBounds.get(0).getPreCouponCode());

			boundcouponCall.setData(boundcouponBean);
			boundcouponCall.callService();

			Logger.verifyEquals("11005", boundcouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("优惠券已被领取过", boundcouponCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BoundCoupon_003() {
		Logger.start(true, "JIRACONN-601用户没有参与过活动，但在其它地方领取过活动优惠券，系统返回用户超出批次领取限制");
		try {

			List<YmtCouponActivityBatch> batchs = new YmtCouponActivityWapper()
					.selectYmtCouponActivityBatchByActivityCode(16);

			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();

			String batchCode = "";

			for (YmtCouponActivityBatch activityBatch : batchs) {
				String batchCodeStr = activityBatch.getCouponBatchCodes();
				String batchCodes[] = batchCodeStr.split(",");
				batchCode = batchCodes[batchCodes.length - 1];
			}

			List<YmtCouponActivityPregenerate> ymtCouponActivityNewUserBounds = new YmtCouponActivityWapper()
					.selectTop1UnUsedPregenerateCodeByActivityCode(16);

			// int userId = 30000000 + Integer.parseInt(TestDataManager.getRandomNum(7));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			
			new CouponServiceCallService().receiveCouponInfo(batchCode, userId);
			
			boundcouponBean.setUserId(userId);
			boundcouponBean.setCouponCode(ymtCouponActivityNewUserBounds.get(0).getPreCouponCode());

			boundcouponCall.setData(boundcouponBean);
			boundcouponCall.callService();

			Logger.verifyEquals("11003", boundcouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("已经领取过", boundcouponCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	public void Tc_BoundCoupon_004() {
		Logger.start(true, "等级为1的用户绑定未绑定优惠券-未生成绑定记录");
		try {

			List<YmtCouponActivityBatch> batchs = new YmtCouponActivityWapper()
					.selectYmtCouponActivityBatchByActivityCode(16);

			List<Integer> receiveCountlist = new ArrayList<>();
			List<Integer> settingIdlist = new ArrayList<>();

			String batchCode = "";

			for (YmtCouponActivityBatch activityBatch : batchs) {
				String batchCodeStr = activityBatch.getCouponBatchCodes();
				String batchCodes[] = batchCodeStr.split(",");
				batchCode = batchCodes[batchCodes.length - 1];

				YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
				YmtCouponSetting setting = new YmtCouponSettingWapper()
						.selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
				settingIdlist.add(setting.getiCouponSettingId());
				receiveCountlist.add(setting.getiReceiveCount());
			}

			List<YmtCouponActivityPregenerate> ymtCouponActivityNewUserBounds = new YmtCouponActivityWapper()
					.selectTop1UnUsedPregenerateCodeByActivityCode(16);

			int userId = 60000000 + Integer.parseInt(TestDataManager.getRandomNum(7));

			boundcouponBean.setUserId(userId);
			boundcouponBean.setCouponCode(ymtCouponActivityNewUserBounds.get(0).getPreCouponCode());
			boundcouponBean.setMobile("18516029204");
			boundcouponCall.setData(boundcouponBean);
			
			boundcouponCall.callService();

			Logger.verifyEquals("12001", boundcouponCall.getCode(), "验证返回StatusCode");
			Logger.verifyEquals("用户存在风险", boundcouponCall.getMessage(), "验证返回Message");

			// 验证数据库落地
			// YmtCouponActivityNewUserBound表
			List<YmtCouponActivityNewUserBound> bounds = new YmtCouponActivityWapper()
					.selectCouponActivityNewUserBoundByUserIdAndActivityCode(userId+"", 16);

			Logger.verifyEquals(0, bounds.size(), "验证数据库表Ymt_CouponActivityNewUserBound生成一条绑定记录");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}